package com.shiliuzi.www.pojo.bo.util;

import com.shiliuzi.www.common.Code;
import com.shiliuzi.www.exception.GlobalException;
import com.shiliuzi.www.pojo.dto.download.DownloadExcelDto;
import com.shiliuzi.www.pojo.po.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DownloadExcelReadBo {

    /**
     * 类型id
     */
    private Integer classId;

    /**
     * 用户id
     */
    private Integer id;

    /**
     * 姓名
     */
    private String userName;

    /**
     * 年级
     */
    private Integer grade;

    /**
     * 学号
     */
    private String number;

    /**
     * 奖惩日期
     */
    private String date;

    /**
     * 奖惩内容
     */
    private String content;

    /**
     * 奖惩原因
     */
    private String reason;

    /**
     * 奖惩金额
     */
    private String money;

    /**
     * 奖惩备注
     */
    private String comment;

    /**
     * 是否撤销
     */
    private Integer isCancel = 0;

    /**
     * 是否申述
     */
    private Integer isInfo = 0;

    /**
     * 附件路径
     */
    private String pathFile;

    /**
     * 判断是否正确，默认设为不正确
     */
    private Integer isRight = 0;

    public DownloadExcelReadBo(Integer classId, DownloadExcelUserBo user, String pathFile, Integer isRight, DownloadExcelDto downloadExcelDto) {
        this.classId = classId;
        this.pathFile = pathFile;
        this.isRight= isRight;
        copyProperties(user);
        copyProperties(downloadExcelDto);
    }

    /**
     * 定制类型转换器
     * @param user
     */
    public void copyProperties(DownloadExcelUserBo user) {
        Class clazz = user.getClass();

        Field[] fields = clazz.getDeclaredFields();

        try {
            for (Field field : fields) {
                String fileName = field.getName();
                String suffix = fileName.substring(0,1).toUpperCase() + fileName.substring(1);

                Method getMethod = clazz.getMethod("get" + suffix);
                Method setMethod = this.getClass().getMethod("set" + suffix, field.getType());

                setMethod.invoke(this,getMethod.invoke(user));
            }
        } catch (Exception e) {
            throw new GlobalException(Code.SYS_ERR,"系统内部出错！excel导入转换器1宕机！");
        }
    }

    /**
     * 定制类型转换器
     * @param downloadExcelDto
     */
    public void copyProperties(DownloadExcelDto downloadExcelDto) {
        Class clazz = downloadExcelDto.getClass();

        Field[] fields = this.getClass().getDeclaredFields();

        try {
            for (Field field : fields) {
                String fileName = field.getName();
                String suffix = fileName.substring(0,1).toUpperCase() + fileName.substring(1);

                Method getSourceMethod = this.getClass().getMethod("get" + suffix);
                if (getSourceMethod.invoke(this) != null) continue;

                Method setSourceMethod = this.getClass().getMethod("set" + suffix, field.getType());
                Method getTargetMethod = clazz.getMethod("get" + suffix);

                Object result = getTargetMethod.invoke(downloadExcelDto);

                if (!fileName.equals("date")) {
                    setSourceMethod.invoke(this, result);
                    continue;
                }

                String strongFile = String.valueOf(result);
                if (!Pattern.compile("^\\d{4}/\\d{2}/\\d{2}$").matcher(strongFile).matches()) {
                    this.isRight = 0;
                    continue;
                }

                strongFile = strongFile.replaceAll("/","-");

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = simpleDateFormat.parse(strongFile);
                if (date.after(new Date())) {
                    this.isRight = 0;
                    continue;
                }
                this.date = strongFile;
            }
        } catch (Exception e) {
            throw new GlobalException(Code.SYS_ERR,"系统内部出错！excel导入转换器2宕机！");
        }
    }
}
